<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>vue学习</title>
    <script src="./js/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <h3>用户登录</h3>
      <div>
        账号：
        <input type="text" v-model="userForm.username" />
      </div>
      <div>
        密码：
        <input type="text" v-model="userForm.password" />
      </div>
      <div>
        <button @click="doLogin">进入系统</button>
      </div>
    </div>
    <script>
      const app = Vue.createApp({
        data() {
          return {
            userForm: {
              username: '',
              password: ''
            }
          }
        },
        methods: {
          doLogin() {
            // console.log(this.userForm)
            const xhr = new XMLHttpRequest()
            xhr.open('GET', '/mock/user.json', true)
            // 0-4 5个状态码  4请求完毕
            xhr.onreadystatechange = () => {
              if (xhr.readyState == 4 && xhr.status == 200) {
                // console.log(JSON.parse(xhr.responseText))
                // 在返回值中找有没有{开头，有表示返回的是一个json字符串，我们就可以转换
                if(xhr.responseText.startsWith('{')){
                  console.log(JSON.parse(xhr.responseText))
                }
              }
            }
            xhr.send(null)
          }
        }
      })
      app.mount('#app')
    </script>
  </body>
</html>
